package com.king.controller;

import com.king.service.WxService;
import com.king.utils.CommonUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;

@RestController
@RequestMapping("/wx")
public class WxController {
    /**
     * 验证消息的确来自微信服务器
     * signature	微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
     * timestamp	时间戳
     * nonce	    随机数
     * echostr	    随机字符串
     *
     * @return
     */
    @GetMapping("/helloWX")
    public void hellWx(HttpServletRequest request, HttpServletResponse responese) throws Exception {
        System.out.println("hello wechat...");

        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");

        if (CommonUtils.isNullOrEmpty(signature)) {
            throw new Exception("signature不能为空！");
        }

        if (CommonUtils.isNullOrEmpty(timestamp)) {
            throw new Exception("timestamp不能为空！");
        }

        if (CommonUtils.isNullOrEmpty(nonce)) {
            throw new Exception("nonce不能为空！");
        }

        if (CommonUtils.isNullOrEmpty(echostr)) {
            throw new Exception("echostr不能为空！");
        }

        System.out.println("signature: " + signature);
        System.out.println("timestamp: " + timestamp);
        System.out.println("nonce: " + nonce);
        System.out.println("echostr: " + echostr);

        try {
            //若确认此次GET请求来自微信服务器，请原样返回echostr参数内容，则接入生效，成为开发者成功，否则接入失败
            if (WxService.CheckSignature(signature, timestamp, nonce)) {
                System.out.println("wx接入成功！");
                PrintWriter out = responese.getWriter();
                out.print(echostr);//原样返回
                out.flush();
                out.close();
            } else {
                System.out.println("wx接入失败！");
            }
        } catch (Exception ex) {
            System.out.println("wx接入失败！");
            ex.printStackTrace();
        }
    }

    /**
     * 接收微信消息及事件推送
     *
     * @param request
     * @param responese
     * @throws Exception
     */
    @PostMapping("/helloWX")
    public void postHelloWx(HttpServletRequest request, HttpServletResponse responese) throws Exception {
        System.out.println("---------------------------------");
        System.out.println("POST,接收到wx公众号消息...");

        //处理乱码问题
        request.setCharacterEncoding("UTF8");
        responese.setCharacterEncoding("UTF8");

        /*String requestXml = CommonUtils.getRequestData(request.getInputStream());
        System.out.println("requestXml: \r\n"+requestXml);*/

        /*Map<String, String> requestMap = CommonUtils.parseRequest(request.getInputStream());
        String xml = WxService.getResponse(requestMap);*/

        Map<String, Object> requestMap = CommonUtils.xml2MapByStream(request.getInputStream());
        System.out.println("requestMap: \r\n"+requestMap);

        String xml = WxService.getResponse(requestMap);
        System.out.println("回复wx公众号消息...");
        System.out.println(xml);

        PrintWriter out = responese.getWriter();
        out.print(xml);//回复wx
        out.flush();
        out.close();
    }
}
